package com.gopay.common.cps.dao.bank.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.bank.BisBankInfoMainDAO;
import com.gopay.common.domain.bis.BisBankInfoMain;

@Repository("bisBankInfoMainDAO")
public class BisBankInfoMainDAOImpl extends BaseDAOImpl<BisBankInfoMain, String>
		implements BisBankInfoMainDAO {

	@Override
	public BisBankInfoMain getByCode(final String bankCode) {
	    
	    return getHibernateTemplate().execute(new HibernateCallback<BisBankInfoMain>() {

            @Override
            public BisBankInfoMain doInHibernate(Session session) throws HibernateException, SQLException {
                Criteria crit = session.createCriteria(BisBankInfoMain.class);
                crit.add(Restrictions.eq("bankCode", bankCode).ignoreCase());
                crit.add(Restrictions.eq("del", false));
                return (BisBankInfoMain)crit.uniqueResult();
            }
        });
	    
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<BisBankInfoMain> findNotDelBanks(List bankCodes) {
		if (bankCodes == null || bankCodes.size() < 1) {
			return null;
		}
		StringBuilder bankCodesStr = new StringBuilder("(");
		for (int i = 0; i < bankCodes.size(); i++) {
			bankCodesStr.append("'").append(bankCodes.get(i)).append("',");
		}
		bankCodesStr.append(")");
		String sql = bankCodesStr.toString();
		sql = sql.replaceAll(",\\)", "\\)");
		String hql = " from BisBankInfoMain  t where t.del=0 and t.bankCode in " + sql;
		return findByHql(hql);
	}

	@Override
	public List<BisBankInfoMain> getBisBankInfoByBankName(final String bankname) {
		String hql = "FROM BisBankInfoMain WHERE del=0 and  bankName like "+"'%"+ bankname + "%'";
		return findByHql(hql);
	}

	@Override
	public List<BisBankInfoMain> getAllBisBankInfo() {
		return findByHql(" from BisBankInfoMain t where t.del = 0");
	}
	
	
	@Override
	public BisBankInfoMain getByCodeOrName(String bankCode,String bankName) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("bankName", bankName);
		param.put("bankCode", bankCode);
		List<BisBankInfoMain> ss = findByHql(
				" from BisBankInfoMain t where (t.bankCode =:bankCode or t.bankName=:bankName) and t.del = 0 ",param);
		return (null == ss || 0 == ss.size()) ? null : ss.get(0);
	}

	@Override
	public BisBankInfoMain getBisBankInfoByAccurateBankName(String bankName) {
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("bankName", bankName);
		List<BisBankInfoMain> ss = findByHql(
				" from BisBankInfoMain t where t.bankName=:bankName and t.del = 0 ",param);
		return (null == ss || 0 == ss.size()) ? null : ss.get(0);
	}
	

}
